<template>
  <div class="register-container">
    <h2>注册</h2>
    <form @submit.prevent="registerUser">
      <div>
        <label for="username">用户名:</label>
        <input type="text" id="username" v-model="username" required />
      </div>
      <div>
        <label for="email">邮箱:</label>
        <input type="email" id="email" v-model="email" required />
      </div>
      <div>
        <label for="password">密码:</label>
        <input type="password" id="password" v-model="password" required />
      </div>
      <button type="submit">注册</button>
    </form>
    <router-link to="/">已有账号? 登录</router-link>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const username = ref('');
const email = ref('');
const password = ref('');

// 注册用户并保存到 localStorage
const registerUser = () => {
  const userInfo = {
    username: username.value,
    email: email.value,
    password: password.value,
  };

  // 将用户信息存储到 localStorage
  localStorage.setItem('userInfo', JSON.stringify(userInfo));
  console.log('用户信息已保存:', userInfo);

  // 清空输入框
  username.value = '';
  email.value = '';
  password.value = '';
};
</script>

<style scoped>
.register-container h2 {
  text-align: center;
}
.register-container {
  position: relative;
  top: 150px;
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.register-container h2 {
  margin-bottom: 20px;
}

.register-container form {
  display: flex;
  flex-direction: column;
}

.register-container label {
  margin-bottom: 5px;
}

.register-container input {
  display: block;
  width: 80%;
  padding: 10px;
  margin: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.register-container button {
  padding: 10px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.register-container button:hover {
  background-color: #0056b3;
}
</style>
